package com.irdstudio.apicenter.console.controller.appframe;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.irdstudio.apicenter.console.controller.MacmsBaseAction;
import com.irdstudio.apicenter.console.dm.AppChannelForward;
import com.irdstudio.apicenter.console.service.appframe.AppChannelForwardService;

/**
 * APP转发渠道管理
 * 
 * @author jr
 * @version 1.0
 * @date 2015-07-06
 */
@Controller
@RequestMapping(value = "/appChannelForward")
public class AppChannelForwardAction extends MacmsBaseAction {
	@Autowired
	private AppChannelForwardService appChannelForwardService;

	@RequestMapping(value = "ACForwardList")
	public String queryACForwardList(HttpServletRequest request) {
		String appId = request.getParameter("appid");
		request.setAttribute("appid", appId);
		return "appframe/createTrade";
	}

	@RequestMapping(value = "jumpCreatePage")
	public String jumpCreatePage(HttpServletRequest request) {
		String appId = request.getParameter("appid");
		request.setAttribute("appid", appId);
		return "appframe/createTradeChannel";
	}

	@RequestMapping(value = "jumpEditPage")
	public String jumpEditPage(HttpServletRequest request) throws SQLException {
		String view = "";
		if (request.getParameter("view") != null) {
			view = request.getParameter("view");
		}

		String forwardChannelId = request.getParameter("channelid");
		AppChannelForward appChannelForward = appChannelForwardService
				.queryWithKeys(forwardChannelId);
		request.setAttribute("appChannelForward", appChannelForward);
		if ("1".equals(view)) {
			return "appframe/viewTradeChannel";
		} else {
			return "appframe/editTradeChannel";
		}

	}

	@RequestMapping(value = "saveAppChannelForward")
	public void saveAppChannelForward(
			@ModelAttribute("appChannelForward") AppChannelForward appChannelForward,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		flag = appChannelForwardService.insert(appChannelForward);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}

	}

	@RequestMapping(value = "updateAppChannelForward")
	public void updateAppChannelForward(
			@ModelAttribute("appChannelForward") AppChannelForward appChannelForward,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		flag = appChannelForwardService.update(appChannelForward);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}

	}

	@RequestMapping(value = "delAppChannelForward")
	public void delAppChannelForward(HttpServletRequest request,
			HttpServletResponse response) throws ClassNotFoundException,
			SQLException {
		int flag = -1;
		String forward_channel_id = request.getParameter("forward_channel_id");
		flag = appChannelForwardService.delete(forward_channel_id);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

}
